using Dapper;
using Eno.Domain.Repositories;
using Npgsql;

namespace Eno.Infrastructure.InfRepositories;

public class CombineUserRoleToDelteteRepository : IUserToRoleRepository
{
    public readonly string _connectionString;  //* 拿的是连接postgresql的配置文件字符串

    public CombineUserRoleToDelteteRepository(string connectionString)
    {
        _connectionString = connectionString;
    }
    public async Task<int> DeleteRole(Guid rId)
    {
        using var conn = new NpgsqlConnection(_connectionString);
        var number = await conn.ExecuteAsync("delete from  app_users_roles where \"RolesID\"=@RoleID", new { RoleID = rId });
        return number;
    }

    public async Task<int> DeleteUser(Guid uId)
    {
        using var conn = new NpgsqlConnection(_connectionString);
        await conn.OpenAsync();
        using var transaction = await conn.BeginTransactionAsync();

        var sql = @"DELETE FROM app_users_roles WHERE ""UsersID"" = @ID ";

        var number = await conn.ExecuteAsync(sql, new { ID = uId }, transaction);
        await transaction.CommitAsync();
        return number;
    }

}